package com.md.notes.model.module.log;

import android.app.Activity;
import android.os.Environment;
import android.widget.Toast;

import com.md.notes.model.module.string.StrBuffer;
import com.md.notes.util.DateUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * 责任链模式
 */
public abstract class LoggerABC {
    private Activity activity;

    public enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        ERROR
    }

    public static final int INFO = 0;
    public static final int DEBUG = 1;
    public static final int ERROR = 2;

    protected int level;
    private File logDir;
    //责任链中的下一个元素
    protected LoggerABC nextLoggerABC;
    protected Exception exception;

    public void setNextLoggerABC(LoggerABC nextLoggerABC) {
        this.nextLoggerABC = nextLoggerABC;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public LoggerABC() {
        try {
            // 获取外部存储根目录
            File rootDir = Environment.getExternalStorageDirectory();
            // 创建 MdNotes 文件夹
            File mdNotesLogDir = new File(rootDir, "MdNotes_Log");
            if (!mdNotesLogDir.exists()) {
                if (!mdNotesLogDir.mkdirs()) {
                    Toast.makeText(activity, "创建目录失败", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
            this.logDir = mdNotesLogDir;


        } catch (Exception e) {
        }
    }

    public void logMessage(int level, String message) {
        if (this.level == level) {
            write(message);
        }
        if (nextLoggerABC != null) {
            nextLoggerABC.logMessage(level, message);
        }
    }

    /**
     * 向txt文件末行追加内容
     *
     * @param filename 文件名称
     * @param content  要追加的内容
     */
    protected void appendToFile(String filename, String content) {
        try {
            StrBuffer sbuffer = new StrBuffer();
            sbuffer.append(filename);
            sbuffer.append("_");
            sbuffer.append(DateUtils.formatDate());
            sbuffer.append(".txt");
            String child = sbuffer.toString();
            File fileLog = new File(logDir, child);
            // 使用try-with-resources语句自动关闭流资源
            try (FileOutputStream fileOutputStream = new FileOutputStream(fileLog, true);
                 // 使用OutputStreamWriter指定编码格式为UTF-8
                 OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
                 // 使用PrintWriter进行格式化输出
                 PrintWriter printWriter = new PrintWriter(outputStreamWriter)) {

                // 写入内容
                printWriter.println("***********************************************");
                printWriter.println(DateUtils.getCurrentDateSSS());
                printWriter.println(content);
                printWriter.println("");

                // 显式刷新缓冲区，确保所有数据都被写入文件
                printWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("写入文件时发生错误：" + e.getMessage());
        }
    }

    protected abstract void write(String message);
}
